/*
 * Copyright (c) 2015 elion. All Rights Reserved.
 */
package com.guoyu.project.web.api;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.collect.Maps;
import com.guoyu.project.common.pagination.Pagination;
import com.guoyu.project.common.utils.JsonUtil;
import com.guoyu.project.common.web.APIResponse;
import com.guoyu.project.model.Attachment;
import com.guoyu.project.model.College_Company;
import com.guoyu.project.model.UserInfo;
import com.guoyu.project.service.College_CompanyService;
import com.guoyu.project.service.FileUploadService;


@Controller
@RequestMapping("api/collegeCompany")
public class CollegeCompanyApiController extends BaseApiController{

    private final static Logger logger = LoggerFactory.getLogger(CollegeCompanyApiController.class);

    @Resource
    private College_CompanyService college_CompanyService;

    @Resource
    private FileUploadService fileUploadService;


    /**
     * 文章列表页
     *
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public Pagination<College_Company> list(@RequestParam(defaultValue = "0") int offset,
            @RequestParam(defaultValue = "10") int limit) {
        Map<String, Object> params = Maps.newHashMap();
        return college_CompanyService.queryAllCollege_Company(offset, limit, params);
    }


    /**
     * 保存今日资讯信息
     *
     * @return
     */
    @RequestMapping("save")
    @ResponseBody
    public APIResponse save(@RequestParam(value = "pictureC", required = false) MultipartFile picture,
    		@RequestParam(value = "savefile1", required = false) MultipartFile fiFile,
    		@RequestParam(value = "savefile2", required = false) MultipartFile seFile,
    		@RequestParam(value = "savefile3", required = false) MultipartFile thFile,
    		College_Company college_company,HttpServletRequest request) {
    	
    	UserInfo sessionUserInfo = getCurrentUser(request);
        int userId = sessionUserInfo.getId();
        college_company.setUserId(userId);
        //文件上传1
        String fiFilePath=fileUploadService.upload(fiFile);
        college_company.setFiFile(fiFilePath);
        //文件上传2
        String seFilePath=fileUploadService.upload(seFile);
        college_company.setSeFile(seFilePath);
        //文件上传3
        String thFilePath=fileUploadService.upload(thFile);
        college_company.setThFile(thFilePath);
    	//图片上传
        String picPath = fileUploadService.upload(picture);
        college_company.setPicture(picPath);
        //设置初始状态
        college_company.setStatus(0);
        
        college_CompanyService.saveCollege_Company(college_company);
        return APIResponse.returnSuccess();
    }

    /**
     * 跳到修改banner信息信息页面
     *
     * @return
     */
    @RequestMapping("query")
    @ResponseBody
    public APIResponse edit(Integer college_id) {
    	College_Company collegeCompany=college_CompanyService.queryById(college_id);
    	return APIResponse.returnSuccess(collegeCompany);
    	 
    }

    /**
     * 更新信息
     *
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public APIResponse update(@RequestParam(value = "file", required = false) MultipartFile picture, 
    		@RequestParam(value = "updatefile1", required = false) MultipartFile fiFile,
    		@RequestParam(value = "updatefile2", required = false) MultipartFile seFile,
    		@RequestParam(value = "updatefile3", required = false) MultipartFile thFile,
    		College_Company college_company) {
    
        logger.info("提交修改信息：{}", JsonUtil.encode(college_company));
        
        String filePath = StringUtils.defaultIfBlank(fileUploadService.upload(picture), college_company.getPicture());
        college_company.setPicture(filePath);
        //文件上传1
        String fiFilePath=fileUploadService.upload(fiFile);
        college_company.setFiFile(fiFilePath);
        //文件上传2
        String seFilePath=fileUploadService.upload(seFile);
        college_company.setSeFile(seFilePath);
        //文件上传3
        String thFilePath=fileUploadService.upload(thFile);
        college_company.setThFile(thFilePath);
        college_CompanyService.updateCollege_Company(college_company);
        return APIResponse.returnSuccess();
    }

    /**
     * 删除信息
     *
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public APIResponse delete(Integer college_id) {
    	college_CompanyService.deleteById(college_id);
        return APIResponse.returnSuccess();
    }
    @RequestMapping("queryByUserId")
	@ResponseBody
	public Pagination<College_Company> queryByUserId(@RequestParam(defaultValue = "0") int offset,
			@RequestParam(defaultValue = "10") int limit,int userId){
		Map<String,Object> params = Maps.newHashMap();
		params.put("userId",userId);
		return college_CompanyService.queryAllCollege_Company(offset, limit, params);
	}

}